import {AsyncStorage} from 'react-native'
import {MessageModel} from "./model";

const STORAGE_KEY = "STORAGE_KEY_message";
export default {
    namespace: 'message',
    state: {
        loading: false,
        list: [],
    },
    reducers: {
        save(state, {payload}) {
            return {...state, ...payload}
        }
    },
    effects: {


        // 保存到本地
        * saveToStorage({payload}, {call}) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload))
        },

        // 本地恢复
        * restoreFromStorage({payload}, {call, put, select}) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            // console.log('恢复', data);
            yield put({
                type: 'save',
                payload: {...JSON.parse(data)}
            });
        },
        //清除数据
        * clear({payload}, {put}) {
        },

        //新消息
        * newMessage({payload}, {put, select}) {
            const message: MessageModel = yield select(state => state.message);
            message.list.push(payload);
            yield put({
                type: 'save',
                payload: {
                    ...message
                }
            });
            yield put({
                type: 'saveMessage'
            })

        },

        //读消息
        * readMessage({payload}, {put, select}) {
            const message: MessageModel = yield select(state => state.message);
            message.list.splice(message.list.indexOf(payload), 1);
            yield put({
                type: 'save',
                payload: {
                    ...message
                }
            });
            yield put({
                type: 'saveToStorage',
                payload: {
                    ...message
                }
            });

        },


        * readMessageFromUser({payload}, {put, select}) {
            const receiver = payload.receiver;
            let message: MessageModel = yield select(state => state.message);
            // console.log('1',message);
            message.list =  message.list.filter((m:messageItem) =>m&&m.username!==receiver );
            // console.log('2',message);
            yield put({
                type: 'save',
                payload: {
                    ...message
                }
            });
            yield put({
                type: 'saveToStorage',
                payload: {
                    ...message
                }
            });
        },

        * saveMessage({payoad}, {put, select}) {
            const message: MessageModel = yield select(state => state.message);
            yield put({
                type: 'saveToStorage',
                payload: {
                    ...message
                }
            });
        },
        * restoreMessage({payload}, {put}) {
            yield put({
                type: 'restoreFromStorage',
                payload: {}
            })
        }
    },
    subscriptions: {
        setup({dispatch}) {
            dispatch({
                type: 'restoreMessage'
            })
        }
    },
};
